import request from '@/utils/request'

// 模拟API数据
const mockApiList = [
  {
    id: 1,
    name: '获取用户列表',
    path: '/api/users',
    method: 'GET',
    description: '获取系统中所有用户的列表',
    status: 1,
    createTime: '2024-01-01 10:00:00',
    requestParams: JSON.stringify({
      page: 1,
      size: 10,
      keyword: 'string'
    }, null, 2),
    responseExample: JSON.stringify({
      code: 200,
      message: '成功',
      data: {
        list: [
          {
            id: 1,
            username: 'admin',
            email: 'admin@example.com'
          }
        ],
        total: 1
      }
    }, null, 2)
  },
  {
    id: 2,
    name: '创建用户',
    path: '/api/users',
    method: 'POST',
    description: '创建新用户',
    status: 1,
    createTime: '2024-01-01 10:30:00',
    requestParams: JSON.stringify({
      username: 'string',
      email: 'string',
      password: 'string',
      nickname: 'string'
    }, null, 2),
    responseExample: JSON.stringify({
      code: 200,
      message: '创建成功',
      data: {
        id: 1,
        username: 'newuser',
        email: 'newuser@example.com'
      }
    }, null, 2)
  },
  {
    id: 3,
    name: '更新用户',
    path: '/api/users/{id}',
    method: 'PUT',
    description: '更新指定用户信息',
    status: 1,
    createTime: '2024-01-01 11:00:00',
    requestParams: JSON.stringify({
      username: 'string',
      email: 'string',
      nickname: 'string'
    }, null, 2),
    responseExample: JSON.stringify({
      code: 200,
      message: '更新成功',
      data: {
        id: 1,
        username: 'updateduser',
        email: 'updated@example.com'
      }
    }, null, 2)
  },
  {
    id: 4,
    name: '删除用户',
    path: '/api/users/{id}',
    method: 'DELETE',
    description: '删除指定用户',
    status: 0,
    createTime: '2024-01-01 11:30:00',
    requestParams: JSON.stringify({}, null, 2),
    responseExample: JSON.stringify({
      code: 200,
      message: '删除成功'
    }, null, 2)
  },
  {
    id: 5,
    name: '用户登录',
    path: '/api/auth/login',
    method: 'POST',
    description: '用户登录接口',
    status: 1,
    createTime: '2024-01-01 12:00:00',
    requestParams: JSON.stringify({
      username: 'string',
      password: 'string'
    }, null, 2),
    responseExample: JSON.stringify({
      code: 200,
      message: '登录成功',
      data: {
        token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
        userInfo: {
          id: 1,
          username: 'admin',
          nickname: '管理员'
        }
      }
    }, null, 2)
  },
  {
    id: 6,
    name: '获取角色列表',
    path: '/api/roles',
    method: 'GET',
    description: '获取系统角色列表',
    status: 1,
    createTime: '2024-01-01 12:30:00',
    requestParams: JSON.stringify({
      page: 1,
      size: 10
    }, null, 2),
    responseExample: JSON.stringify({
      code: 200,
      message: '成功',
      data: {
        list: [
          {
            id: 1,
            name: '管理员',
            code: 'admin',
            description: '系统管理员'
          }
        ],
        total: 1
      }
    }, null, 2)
  }
]

// 模拟API接口
export function getApiList(params) {
  return new Promise((resolve) => {
    setTimeout(() => {
      let filteredList = [...mockApiList]
      
      // 模拟搜索过滤
      if (params.name) {
        filteredList = filteredList.filter(item => 
          item.name.toLowerCase().includes(params.name.toLowerCase())
        )
      }
      
      if (params.method) {
        filteredList = filteredList.filter(item => item.method === params.method)
      }
      
      if (params.status !== undefined && params.status !== '') {
        filteredList = filteredList.filter(item => item.status == params.status)
      }
      
      // 模拟分页
      const page = params.page || 1
      const size = params.size || 10
      const start = (page - 1) * size
      const end = start + size
      const list = filteredList.slice(start, end)
      
      resolve({
        code: 200,
        message: '成功',
        data: {
          list,
          total: filteredList.length
        }
      })
    }, 300)
  })
}

export function createApi(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const newApi = {
        ...data,
        id: Date.now(),
        status: 1,
        createTime: new Date().toLocaleString()
      }
      mockApiList.unshift(newApi)
      
      resolve({
        code: 200,
        message: '创建成功',
        data: newApi
      })
    }, 500)
  })
}

export function updateApi(id, data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const index = mockApiList.findIndex(item => item.id == id)
      if (index > -1) {
        mockApiList[index] = { ...mockApiList[index], ...data }
        resolve({
          code: 200,
          message: '更新成功',
          data: mockApiList[index]
        })
      } else {
        resolve({
          code: 404,
          message: 'API不存在'
        })
      }
    }, 500)
  })
}

export function deleteApi(id) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const index = mockApiList.findIndex(item => item.id == id)
      if (index > -1) {
        mockApiList.splice(index, 1)
        resolve({
          code: 200,
          message: '删除成功'
        })
      } else {
        resolve({
          code: 404,
          message: 'API不存在'
        })
      }
    }, 500)
  })
}

export function testApi(id, params) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const api = mockApiList.find(item => item.id == id)
      if (api) {
        // 模拟API测试结果
        const result = {
          code: 200,
          message: '测试成功',
          data: {
            timestamp: new Date().toISOString(),
            method: api.method,
            path: api.path,
            requestParams: params,
            responseTime: Math.floor(Math.random() * 500) + 100 + 'ms',
            status: 'success'
          }
        }
        
        // 随机模拟一些失败情况
        if (Math.random() < 0.1) {
          result.code = 500
          result.message = '服务器内部错误'
          result.data.status = 'error'
        }
        
        resolve(result)
      } else {
        resolve({
          code: 404,
          message: 'API不存在'
        })
      }
    }, Math.floor(Math.random() * 2000) + 500) // 模拟网络延迟
  })
}

// 流程图相关接口
export function saveFlowchart(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const flowchart = {
        ...data,
        id: Date.now(),
        createTime: new Date().toLocaleString(),
        updateTime: new Date().toLocaleString()
      }
      
      // 保存到本地存储
      const savedFlowcharts = JSON.parse(localStorage.getItem('flowcharts') || '[]')
      savedFlowcharts.unshift(flowchart)
      localStorage.setItem('flowcharts', JSON.stringify(savedFlowcharts))
      
      resolve({
        code: 200,
        message: '保存成功',
        data: flowchart
      })
    }, 500)
  })
}

export function getFlowchartList() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const flowcharts = JSON.parse(localStorage.getItem('flowcharts') || '[]')
      resolve({
        code: 200,
        message: '成功',
        data: flowcharts
      })
    }, 300)
  })
}

export function deleteFlowchart(id) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const flowcharts = JSON.parse(localStorage.getItem('flowcharts') || '[]')
      const filteredFlowcharts = flowcharts.filter(item => item.id != id)
      localStorage.setItem('flowcharts', JSON.stringify(filteredFlowcharts))
      
      resolve({
        code: 200,
        message: '删除成功'
      })
    }, 300)
  })
}
